package com.chinamobile.push


import android.view.LayoutInflater

import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView
import com.chinamobile.push.databinding.ItemDriverBinding
import com.chinamobile.push.databinding.ItemPassengerBinding

import com.cmcc.shared.widget.DriverCardFragment

import com.cmcc.shared.widget.PassengerCardFragment

class MyAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val currentList = mutableListOf<Card>()
    var supportFragmentManager: FragmentManager? = null

    override fun getItemCount(): Int {
        return currentList.size
    }

    fun setList(list: List<Card>) {
        currentList.clear()
        currentList.addAll(list)
        notifyDataSetChanged()
    }


    fun setFragment(supportFragmentManager: FragmentManager) {
        this.supportFragmentManager = supportFragmentManager
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        if (viewType == R.layout.item_driver) {
            return DriverHolder(
                ItemDriverBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        } else {
            return PassengerHolder(
                ItemPassengerBinding.inflate(
                    LayoutInflater.from(
                        parent.context
                    ), parent, false
                )
            )
        }


    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val model = currentList[position]
        val currentType = getItemViewType(position)
        if (currentType == R.layout.item_driver && holder is DriverHolder) {
            var driverCardFragment =
                supportFragmentManager?.findFragmentById(R.id.driverFragment) as DriverCardFragment

        } else if (currentType == R.layout.item_passenger && holder is PassengerHolder) {
            var passengerCardFragment =
                supportFragmentManager?.findFragmentById(R.id.passengerFragment) as PassengerCardFragment
        }
    }

    /**
     * 判断是否是司机
     */
    override fun getItemViewType(position: Int): Int {
        return if (currentList[position].isDriver)
            R.layout.item_driver
        else
            R.layout.item_passenger
    }

}


class DriverHolder(val binding: ItemDriverBinding) : RecyclerView.ViewHolder(binding.root) {
    fun bind() {
        with(binding) {
            executePendingBindings()
        }
    }
}

class PassengerHolder(val binding: ItemPassengerBinding) : RecyclerView.ViewHolder(binding.root) {
    fun bind() {
        with(binding) {
            executePendingBindings()
        }
    }
}